package com.zyj.ssm.controller;  
  
import javax.annotation.Resource;  
import javax.servlet.http.HttpServletRequest;  
  








import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;

import com.zyj.ssm.pojo.User;
import com.zyj.ssm.service.IUserService;
  
  
@Controller  
@RequestMapping("/user")  
public class UserController {  
    @Resource  
    private IUserService userService;  
      
    @RequestMapping("/showUser")  
    public String toIndex(HttpServletRequest request,Model model){  
        int userId = Integer.parseInt(request.getParameter("id"));
        System.out.println(userId);
        User user = this.userService.getUserById(userId);  
        model.addAttribute("user", user);  
        return "showUser";  
    }  
    
    @RequestMapping("/login")  
    public String login(){  
        return "login"; 
    } 
    @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)  
    public String homePage(ModelMap model) {  
        model.addAttribute("greeting", "Hi, Welcome to mysite. ");  
        return "welcome";  
    }  
   
    @RequestMapping(value = "/admin", method = RequestMethod.GET)  
    public String adminPage(ModelMap model) {  
        model.addAttribute("user", getPrincipal());  
        return "admin";  
    }  
   
    @RequestMapping(value = "/db", method = RequestMethod.GET)  
    public String dbaPage(ModelMap model) {  
        model.addAttribute("user", getPrincipal());  
        return "dba";  
    }  
   
    @RequestMapping(value="/logout", method = RequestMethod.GET)  
       public String logoutPage (HttpServletRequest request, HttpServletResponse response) {  
    	Authentication auth = SecurityContextHolder.getContext().getAuthentication();  
        if (auth != null){      
            new SecurityContextLogoutHandler().logout(request, response, auth);  
        }  
        return "redirect:/login?logout";   
       }  
   
    @RequestMapping(value = "/Access_Denied", method = RequestMethod.GET)  
    public String accessDeniedPage(ModelMap model) {  
        model.addAttribute("user", getPrincipal());  
        return "accessDenied";  
    }  
       
    private String getPrincipal(){  
        String userName = null;  
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();  
   
        if (principal instanceof UserDetails) {  
            userName = ((UserDetails)principal).getUsername();  
        } else {  
            userName = principal.toString();  
        }  
        return userName;  
    }  
}  
